﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SchoolManagementSystem
{
    public partial class KaryawanEdit : Form
    {
        private Karyawan parent = null;
        private DataTable dTable = null;
        private int rowIndex = -1;

        public KaryawanEdit()
        {
            InitializeComponent();
        }

        public KaryawanEdit(Karyawan parent_instance, DataTable emp_dataTable, int editRowIndex,
            string emp_name, 
            string emp_gender, 
            object emp_birthdate,
            string emp_birthplace,
            string emp_address,
            string emp_role,
            string emp_edu,
            object emp_start,
            string emp_duration,
            string emp_yayasan,
            string emp_phone,
            string emp_mobile,
            string emp_email)
        {
            InitializeComponent();

            this.parent = parent_instance;
            this.dTable = emp_dataTable;
            this.rowIndex = editRowIndex;

            // Fill field
            this.txtNama.Text = emp_name;
            if (emp_gender == "L")
                this.cmbKelamin.SelectedIndex = 0;
            else if (emp_gender == "P")
                this.cmbKelamin.SelectedIndex = 1;
            this.birthdayBox.Value = (DateTime)emp_birthdate;
            this.textBirthPlace.Text = emp_birthplace;
            this.txtAlamat.Text = emp_address;
            this.textJabatan.Text = emp_role;
            this.textPendidikan.Text = emp_edu;
            this.startWorkBox.Value = (DateTime)emp_start;
            this.txtSK.Text = emp_yayasan;
            this.textPhone.Text = emp_phone;
            this.textMobile.Text = emp_mobile;
            this.textEmail.Text = emp_email;
        }

        private void btnBatal_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnTambah_Click(object sender, EventArgs e)
        {
            // Edit DataTable
            this.dTable.Rows[this.rowIndex]["emp_name"] = this.txtNama.Text;
            if (this.cmbKelamin.Text == "Laki-laki")
            {
                this.dTable.Rows[this.rowIndex]["emp_gender"] = "L";
            }
            else
            {
                this.dTable.Rows[this.rowIndex]["emp_gender"] = "P";
            }
            this.dTable.Rows[this.rowIndex]["emp_birthdate"] = this.birthdayBox.Value.Date;
            this.dTable.Rows[this.rowIndex]["emp_birthplace"] = this.textBirthPlace.Text;
            this.dTable.Rows[this.rowIndex]["emp_address"] = this.txtAlamat.Text;
            this.dTable.Rows[this.rowIndex]["emp_role"] = this.textJabatan.Text;
            this.dTable.Rows[this.rowIndex]["emp_edu"] = this.textPendidikan.Text;
            this.dTable.Rows[this.rowIndex]["emp_start"] = this.startWorkBox.Value.Date;
            this.dTable.Rows[this.rowIndex]["emp_duration"] = num1.Value + " - " + num2.Value;
            this.dTable.Rows[this.rowIndex]["emp_yayasan"] = this.txtSK.Text;
            this.dTable.Rows[this.rowIndex]["emp_phone"] = this.textPhone.Text;
            this.dTable.Rows[this.rowIndex]["emp_mobile"] = this.textMobile.Text;
            this.dTable.Rows[this.rowIndex]["emp_email"] = this.textEmail.Text;

            // Update Database
            Database.getInstance().updateDB(this.dTable);

            var result = MessageBox.Show("Edit karyawan berhasil !", "Sukses !", MessageBoxButtons.OK, MessageBoxIcon.Information);
            if (result == System.Windows.Forms.DialogResult.OK)
            {
                // Close form
                this.Close();
            }
        }

        private Boolean checkFieldData()
        {
            if (this.txtNama.Text == "" ||
                this.cmbKelamin.Text == "" ||
                this.textBirthPlace.Text == "" ||
                this.textJabatan.Text == "" ||
                this.textPendidikan.Text == "" ||
                this.txtAlamat.Text == "" ||
                this.txtSK.Text == "" ||
                this.textPhone.Text == "" ||
                this.textMobile.Text == "" ||
                this.textEmail.Text == "")
            {
                return false;
            }
            return true;
        }

        private void KaryawanEdit_Load(object sender, EventArgs e)
        {
            DateTime date = DateTime.Now;
            if (date.Month < 7)
            {
                num1.Value = date.Year - 1;
                num2.Value = num1.Value + 1;
            }
        }

        private void num1_ValueChanged(object sender, EventArgs e)
        {
            num2.Value = num1.Value + 1;
        }

        private void num2_ValueChanged(object sender, EventArgs e)
        {
            num1.Value = num2.Value - 1;
        }
    }
}
